package factory.one.utils;

import factory.one.base.Constant;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

public class SecurityUtil {

    public static String getUserName(){
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    public static boolean isAdmin(){
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority a : authorities){
            if(a.getAuthority().equals(Constant.ROLE_ADMIN)){
                return true;
            }
        }
        return false;
    }
}
